package com.demo.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lmn on 2018/4/8.
 */
@ControllerAdvice
public class ExceptionHandler {
    public static final String ERROR_VIEW = "error";

    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest request,Exception e){
        e.printStackTrace();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception",e);
        modelAndView.addObject("url",request.getRequestURI());
        //注意将地址放到modelandview中
        modelAndView.setViewName(ERROR_VIEW);
        return modelAndView;
    }
}
